package html.elements;

import html.ParseErrors;

import java.util.Dictionary;
import java.util.HashMap;
import java.util.Map;

public class Node
{
	protected NodeType nodeType;
	protected String nodeName;
	protected String originalNodeText;
	protected String originalText;
	protected Map<String, String> attrs = new HashMap<String, String>();

	protected Node()
	{
	}

	protected NodeType getNodeType()
	{
		return nodeType;
	}

	public String getOriginalText()
	{
		return originalText;
	}

	public static Node getInstance(String tagText, String text, ParseErrors errors)
	{
		Node node = new Node();
		node.originalNodeText = tagText;
		node.originalText = text;

		if (tagText.startsWith("/"))
		{
			node.nodeType = NodeType.END_ELEMENT;
			node.nodeName = tagText.substring(1, getWord(tagText, 1));
			System.out.printf(
					"tagText: !%s!\ntext: !%s!\ntagName: !%s!\n--------------\n", tagText, text, node.nodeName);
			return node;
		}
		else if (tagText.endsWith("/"))
			node.nodeType = NodeType.SINGLE_TAG;
		else
			node.nodeType = NodeType.START_ELEMENT;

		//System.out.println(tagText+" : "+text);
		int iSt = getWord(tagText, 0);
		node.nodeName = tagText.substring(0, iSt);

		System.out.printf(
				"tagText: !%s!\ntext: !%s!\ntagName: !%s!\n--------------\n", tagText, text, node.nodeName);

		return node;
	}

	private static int getWord(String str, int start)
	{
		int i = start;
		for (; i < str.length(); i++)
			if (str.charAt(i) == ' ')
				return i;
		return i;
	}
}
